<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: filter.php 140 2013-09-22 07:44:23Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 140 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-22 07:44:23 +0000 (Sun, 22 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

$box_smarty = new Smarty();

$box_smarty->assign('tpl_path','templates/'.CURRENT_TEMPLATE.'/');
$box_smarty->assign('html_tpl_path', CURRENT_TEMPLATE.'/html');
$box_smarty->assign('language', $_SESSION['language']);

if(isset($_GET['cPath'])) {
	$cat_id_temp = explode('_', $_GET['cPath']);
	$cat_id_temp = array_reverse($cat_id_temp);
	$cat_id = $cat_id_temp[0];
} else
	$cat_id = '0';

if(isset($_GET['cPath']) && $category_depth == 'products')
	$box_smarty->assign('FORM_FILTER', draw_form('product_filter', href_link(FILENAME_DEFAULT, 'cPath='.$_GET['cPath']), 'GET'));
else
	$box_smarty->assign('FORM_FILTER', draw_form('product_filter', 'product_filter.php', 'GET'));

$get_filter = $db->db_query("SELECT filter_id, filter_name, cat_id FROM ".TABLE_PRODUCTS_FILTERS." WHERE parent_id = '0'");

require_once(DIR_FS_INC.'inc.get_subcategories.php');
get_subcategories($sub_categories, $cat_id);
$sub_categories[] = $cat_id;

function countFilter2Cat($filter) {
	global $db, $sub_categories, $filter_products;
	
	$count = '';

	for($i = 0; $i < sizeof($sub_categories); $i++) {
		$pf = $db->db_query("SELECT
								p2f.products_id
							FROM
								".TABLE_PRODUCTS_TO_FILTERS." p2f,
								".TABLE_PRODUCTS_TO_CATEGORIES." p2c,
								".TABLE_PRODUCTS." p
							WHERE
								p2c.categories_id = '".$sub_categories[$i]."'
							AND
								p2c.categories_id !='0'
							AND
								p2f.products_id = p2c.products_id
							AND
								p.products_status = '1'
								".$filter_products."
							AND
								p.products_id = p2c.products_id
							AND
								p2f.filter_id = '".$filter."'
							GROUP BY
								p2f.products_id");
								
		$count += $pf->_numOfRows;
	}
	return $count;
}

if($get_filter->_numOfRows) {
	while(!$get_filter->EOF) {
		$active = false;
		$filter_on = false;
		$active_filter = '';
		$sub = '';
		$total_filter = '';
		
		if(($get_filter->fields['cat_id'] !='' && $get_filter->fields['cat_id'] !='0')) {
			$cat_db = explode('|', $get_filter->fields['cat_id']);
			if(in_array($cat_id, $cat_db) OR $PHP_SELF == 'product_filter.php')
				$active = true;
		}

		if($active || $get_filter->fields['cat_id'] =='0') {
			if(isset($_GET['filter']) && is_array($_GET['filter']) && in_array($get_filter->fields['filter_id'], $_GET['filter'])) {
				$active_filter = ' checked=""';
				$sub_class = 'filter_open';

			} else
				$sub_class = 'filter_close';
								
			$sub_filter = $db->db_query("SELECT 
												filter_id,
												filter_name
											FROM 
												".TABLE_PRODUCTS_FILTERS." 
											WHERE 
												parent_id = '".$get_filter->fields['filter_id']."'");

			if($sub_filter->_numOfRows) {
				
				$counter = '';
				while(!$sub_filter->EOF) {
					$active_sub_filter = '';

					if(isset($_GET['sub_filter']) && is_array($_GET['sub_filter']) && in_array($sub_filter->fields['filter_id'], $_GET['sub_filter']))
						$active_sub_filter = ' checked=""';

					$get_count = $db->db_query("SELECT 
													products_id
												FROM 
													".TABLE_PRODUCTS_TO_FILTERS."
												WHERE 
													filter_id = '".$sub_filter->fields['filter_id']."'
												GROUP BY
													products_id");
													
					$count = countFilter2Cat($sub_filter->fields['filter_id']);
					if($count < 1)
						$count = '0';
					else {
					 	$filter_on = true;
					 	$total_filter += $count;
					}
					$sub[] = array('filter_id' => $sub_filter->fields['filter_id'], 
									'name' => $sub_filter->fields['filter_name'],
									'count' => $count, 
									'checked' => $active_sub_filter,
									'disabled' => ($count < 1 ? ' disabled=""' : ''),
									'class' => ($count < 1 ? ' class="filter_disabled"' : ''),
									'subs' => '');

					$sub_filter->MoveNext();
				}
				if(isset($_GET['sub_filter']))
					$box_smarty->assign('del_filter', '<input class="css_img_button" type="submit" value="X" title="Filter löschen" name="delete_filter" />');
			}
			
			if(is_array($sub)) {
				$filter[] = array('filter_id' => urldecode($get_filter->fields['filter_id']), 
									'name' => $get_filter->fields['filter_name'],
									'clean_name' => get_url_friendly_text(strtolower(str_replace(' ', '', $get_filter->fields['filter_name']))),
									'checked' => $active_filter,
									'disabled' => ($filter_on == false ? ' disabled=""' : ''),
									'total' => $total_filter,
									'sub_class' => $sub_class,
									'subs' => $sub);
			}
		}					
		$get_filter->MoveNext();
	}
	$box_smarty->assign('filters', $filter);
}

$box_smarty->assign('SUBMIT_BUTTON', image_submit('button_send.gif', 'Produkte suchen'));
$box_smarty->caching = false;
$box_content = $box_smarty->fetch(CURRENT_TEMPLATE.'/boxes/box_filter.html');
?>